#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "DrawWidget.h"
#include "GeoJsonReader.h"
#include "GeoSLDReader.h"
#include <QDebug>
#define mout qDebug() <<"["<<__FILE__<<":"<<__LINE__<<"]"

//slotMapTreeMenuDraw

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow){

    map = new GeoMap();
    ui->setupUi(this);
    ui->tabWidget->clear();
    //信号和槽函数相连接
    connect(ui->LoadJson,&QAction::triggered,this,&MainWindow::loadJson);
    connect(ui->LoadSLD,&QAction::triggered,this,&MainWindow::loadSLD);
    connect(ui->DrawMap,&QAction::triggered,this,&MainWindow::drawMap);

}

MainWindow::~MainWindow(){
    delete map;
    delete ui;
}

//json文件读取
void MainWindow::loadJson()
{
    QString path = QFileDialog::getOpenFileName(this,"openFile","/","JsonFile(*.geojson);;AllFile(*.*)");
    if(path.isNull()){
        sendInfo("路径为空");
        return;
    }
    GeoLayer *layer = new GeoLayer();
    layer->addProperties("name",path.split("/").last());
    layer->addProperties("FilePath",path);
    //读json
    GeoJsonReader *jsonReader = new GeoJsonReader(path);
    if(jsonReader->read(layer)){
        sendInfo("Json文件添加成功");
    }else {
        sendInfo("添加文件出错");
    }

    //调试对象数量

    map->addLayer(layer);
    delete jsonReader;
}

//sld读取
void MainWindow::loadSLD()
{
    QString path = QFileDialog::getOpenFileName(this,"openFile","/","SldFile(*.sld);;AllFile(*.*)");
    if(path.isNull()){
        sendInfo("路径为空");
        return;
    }
    GeoLayer *layer = new GeoLayer();
    //读sld
    GeoSLDReader* sldReader = new GeoSLDReader(path);
    if(sldReader->read(layer)){
        sendInfo("SLD文件添加成功");
    }else {
        sendInfo("添加文件出错");
    }
    map->addLayer(layer);
    delete sldReader;
}


//传递地图数据
void MainWindow::drawMap(){
    DrawWidget* openGLWidget = new DrawWidget(map);
    ui->tabWidget->addTab(openGLWidget,"map");
}

//输出状态信息
void MainWindow::sendInfo(QVariant msg)
{
    ui->textBrowser->append(msg.toString());
    ui->textBrowser->append("");
}

